package org.jeecg.modules.activiti.listener;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.activiti.entity.ActBusiness;
import org.jeecg.modules.activiti.service.Impl.ActBusinessServiceImpl;
import org.jeecg.modules.zcgl.entity.AssetFixed;
import org.jeecg.modules.zcgl.service.IAssetFixedService;
import org.springframework.stereotype.Component;

/**
 * 固定资产生效表单审批后监听
 */
@Component
public class AssetFixedTakeEffectListener implements ExecutionListener {


    ActBusinessServiceImpl actBusinessService;
    private IAssetFixedService assetFixedService;

    public void notify(DelegateExecution execution) throws Exception {
        if (assetFixedService == null) {
            assetFixedService = (IAssetFixedService) SpringContextUtils.getBean("assetFixedServiceImpl");
        }
        if (actBusinessService == null) {
            actBusinessService = (ActBusinessServiceImpl) SpringContextUtils.getBean("actBusinessServiceImpl");
        }
        String eventName = execution.getEventName();
        //start
        if (EVENTNAME_START.equals(eventName)) {
        } else if (EVENTNAME_END.equals(eventName)) {
            ActBusiness actBusiness = actBusinessService.getById(execution.getProcessBusinessKey());
            if (actBusiness != null && "zcgl_asset_fixed".equalsIgnoreCase(actBusiness.getTableName())) {
                AssetFixed assetFixed = assetFixedService.getById(actBusiness.getTableId());
                if(assetFixed !=null){
                    //状态 0-未生效 1-生效 2-借出 3-维修中 4-报废
                    assetFixedService.update(new LambdaUpdateWrapper<AssetFixed>().set(AssetFixed::getStatus,"1").eq(AssetFixed::getId,assetFixed.getId()));
                }
            }
        }


    }
}
